/*******************    (C) COPYRIGHT SUNSHINE SILICON    **********************
* File Name: HS6200_demo.c
* Description:

    HS6200 Demo
    
*  Copyright (C) 2014-2035 Sunshine Silicon Corporation (www.sunsili.com), All Rights Reserved
*  SPDX-License-Identifier: Apache-2.0
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may
*  not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*  This file is part of HS6200 SDK
*                                                                                                                                  
 *  sbit HS6200_CE = P1^0;
    sbit HS6200_CS = P1^1;
    sbit SPI_SCK = P1^2;
    sbit SPI_MOSI = P1^3;
    sbit SPI_MISO = P1^4;                                                                                                                                
 *                                    ___________________________                           \ /    
 *                                   |                           |        _____________      |                             
 *                                   |                           |       |             |     |                                  
 *                                   |                     P1.0  | --->  |  nCE        |-----|                                     
 *                                   |                S  | P1.2  | --->  |  SCK | S    |   H                                     
 *                                   |                P  | P1.4  | <---  |  MISO| P    |   S                                     
 *                                   |                I  | P1.3  | --->  |  MOSI| I    |   6  (as PRX&&PTX)                     
 *                                   |                   | P1.1  | --->  |  nSS |      |   2                                     
 *             /-------------------- | VBUS                NC    | <---  |  IRQ        |   0                                     
 *      P ____/RX________USB to_____ | TX                        |       |_____________|   0                                     
 *      C     \RX        UART        | RX                        |                                               
 *             \-------------------- | GND                       |                                         
 *                                   |                           |                                     
 *                                   |                           |                                                     
 *                                   |                           |                                           
 *                                   |                           |                                                              
 *                                   |                     P1.6  | ---> SYSCLK Output,used for test.                            
 *                                   |                           |                                                                  
 *                                   |                           |                                                               
 *                                   |                     P0.0  | ---> LED1 ( LED On when P2.2 output 0 )                       
 *                                   |                     P0.1  | ---> LED2 ( LED On when P2.3 output 0 )                       
 *                                   |                           |                                                               
 *                                   |___________________________|                                                                   
 *                                                                                                                                 
 *                                             N73E003                                                                                        
 *                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
 *  
*
* Version    Date       Auther      Reversed History
  ----------------------------------------------------------------------------
  V1.0.0    2021-10-20  Lojam Fan  Fisrt Created 
  
* (C) Sunshine Silicon Corporation 
*  Website: http://www.sunsili.com
*  E-Mail : fan@sunsili.com

*******************************************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "config.h"
#include "HS6200.h"
#include "delay.h"


//#define APP_LOG_ENABLE     1
//#if APP_LOG_ENABLE 
//#define APP_LOG_DEBUG(...)       uart_printf(__VA_ARGS__)
//#else 
//#define APP_LOG_DEBUG(...)       nop()
//#endif

uint8_t TXBuffer[2] = {0xAA, 0xAA};
uint8_t rx_buf[16];

sbit LED_RED = P0^0;
sbit LED_GRN = P0^1;  
sbit LED_TEST = P1^6;

#define setGPIO_LED_RED_OUT()    P00_PushPull_Mode
#define setGPIO_LED_GRN_OUT()    P01_PushPull_Mode
#define setGPIO_LED_TEST_OUT()   P16_PushPull_Mode
#define LED_RED_ON()    LED_RED = 0
#define LED_GRN_ON()    LED_GRN = 0
#define LED_TEST_ON()   LED_TEST = 0
#define LED_RED_OFF()    LED_RED = 1
#define LED_GRN_OFF()    LED_GRN = 1
#define LED_TEST_OFF()   LED_TEST = 1

/************************************************************************************************************
*    led_init 
************************************************************************************************************/
void led_init(void)
{
    LED_RED = 0;
    LED_GRN = 0;
    LED_TEST = 0;
    setGPIO_LED_RED_OUT();
    setGPIO_LED_GRN_OUT();
    setGPIO_LED_TEST_OUT();
    LED_RED = 1;
    LED_GRN = 1;
    LED_TEST = 1;
}

/***********************************************************************
	WDT CONFIG enable 
	warning : this macro is only when ICP not enable CONFIG WDT function
	copy this marco code to you code to enable WDT reset.
************************************************************************/
void Enable_WDT_Reset_Config(void)
{
    set_IAPEN;
    IAPAL = 0x04;
    IAPAH = 0x00;
    IAPFD = 0x0F;
    IAPCN = 0xE1;
    set_CFUEN;
    set_IAPGO;                                  //trigger IAP
    while((CHPCON&SET_BIT6)==SET_BIT6);         //check IAPFF (CHPCON.6)
    clr_CFUEN;
    clr_IAPEN;

    TA=0xAA;
    TA=0x55;
    WDCON=0x06;		        //Setting WDT prescale   WDPS=0x07(111) div=256 WDTimeout:1.638s Info:datasheetPage118
    set_WDCLR;							    //Clear WDT timer
    while((WDCON|~SET_BIT6)==0xFF);			//confirm WDT clear is ok before into power down mode
    EA = 1;
    set_WDTR;								//WDT run
}

/************************************************************************************************************
*    Main function 
************************************************************************************************************/

void main(void)
{
    uint32_t cnt;
    uint8_t i, recvLen;
 #ifdef USE_SOFT_SPI
    soft_spi_init();
 #endif
    InitialUART0_Timer1(9600);	    //UART0 Baudrate from Timer1   
    printf ("*=======================================*\n");
    printf ("*  *  Name: HS6200 SDK Sample Code.      *\n");
    printf ("*  * (C) Sunshine Silicon Corporation    *\n");
    printf ("*  *  Website: http://www.sunsili.com    *\n");
    printf ("*  *   E-Mail : fan@sunsili.com          *\n");
    printf ("*========================================*\n");
    printf ("* HS6200 receive data and uart  transmit the same data to PC .*\n");

    led_init();
//    Enable_WDT_Reset_Config();
    printf("HS6200 init .... \n");   
    HS6200_Init();
    printf("HS6200 Init Sucesss! \n");
    LED_RED = 0;


    for( ; ;)
    {
        HS6200_ModeSwitch(Rf_PTX_Mode);
        HS6200_Clear_All_Irq();
        HS6200_Flush_Tx();
        HS6200_SendPack(HS6200_W_TX_PAYLOAD , TXBuffer, 2);
        LED_TEST_ON();
        Delay_ms(10);
        
        HS6200_ModeSwitch(Rf_PRX_Mode);
        HS6200_Clear_All_Irq();
        HS6200_Flush_Rx();
        LED_GRN_OFF();
        cnt = 1;
        while(cnt++<100)
        {
            Delay_ms(1);
            recvLen = HS6200_RecivePack(rx_buf);
                
            if(	recvLen )
            {                              
                HS6200_Clear_All_Irq();
                HS6200_Flush_Rx();  
                HS6200_ModeSwitch(Rf_PTX_Mode);
                HS6200_Clear_All_Irq();
                HS6200_Flush_Tx();
                HS6200_SendPack(HS6200_W_TX_PAYLOAD , rx_buf, recvLen);       
//                APP_LOG_DEBUG("HS6200 Recv \n");
                printf("HS6200 Recv: ");
                for(i=0; i<recvLen; i++)
                {                
//                    Send_Data_To_UART0(rx_buf[i]);   
                      printf("%x ", (int)rx_buf[i]);                   
                } 
                printf("\n");
                break;
            }         
        }
//        Send_Data_To_UART0(cnt);  
        printf("Cnt: %d \n", (int)cnt);
        if(	recvLen )
        {                              
           LED_GRN_ON(); 
        } 

        LED_TEST_OFF();
        Delay_ms(100);
        set_WDCLR;	    //Clear WDT timer
    }
}

/******************* (C) COPYRIGHT SUNSHINE SILICON  **************************
********************        END OF FILE HS6200_demo.c        *******************/

